/*
 * Copyright (c) 2025 [ByteDance Ltd. and/or its affiliates.] and/or its affiliates
 * SPDX-License-Identifier: LGPL v2.1+
*/
#include "FFmpegIOCryptorUtils.h"

bool FFmpegIOCryptorUtils::findTag(AVIOContext *pb, uint32_t targetTagName, uint32_t maxBytes, uint32_t *targetTagSize) {
    uint32_t currentBytes = 0;
    while (!avio_feof(pb) && currentBytes < maxBytes) {
        auto tagSize = avio_rb32(pb);
        if (!tagSize) break;

        currentBytes += 4;

        auto tagName = avio_rb32(pb);
        if (!tagName) break;

        currentBytes += 4;

        if (tagName == targetTagName) {
            *targetTagSize = tagSize;
            return true;
        } else {
            avio_skip(pb, tagSize - 8);
            currentBytes += tagSize - 8;
        }
    }

    return false;
}
